package feeinterface.process;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.ulwx.tool.ArrayUtils;
import com.ulwx.tool.CollectionUtils;
import com.ulwx.tool.IOUtils;
import com.ulwx.tool.NetUtils;
import com.ulwx.tool.StringUtils;
import com.ulwx.type.TObject;

import feeinterface.protocol.req.Exchange;
import feeinterface.protocol.req.ProBodyStream;
import feeinterface.protocol.req.Protocol;


public class InitProcessor extends Processor {
	private static Logger log = Logger.getLogger(InitProcessor.class);

	@Override
	public void process(HttpServletRequest request,
			Map<String, String[]> parmMap,TObject bodyBytes) throws Exception {
		// TODO Auto-generated method stub

		if (log.isInfoEnabled()) {

	
			Enumeration headerNames = request.getHeaderNames();
			String headerValues = "";
			while (headerNames.hasMoreElements()) {
				String headerName = (String) headerNames.nextElement();
				headerValues = headerValues + headerName + ":"
						+ request.getHeader(headerName) + ";";

			}
			// log.debug();
			String url = StringUtils.trim(request.getRequestURI());
			log.debug("request URL=" + url);
			log.debug("HTTP Headers:" + headerValues);
			log.debug("++++++++query String=" + request.getQueryString());
		}


		try {
			
			Map<String, String[]> bodyMap = new HashMap<String, String[]>();
			Map<String, String[]> bodyMap1 = new HashMap<String, String[]>();
			Map<String, String[]> bodyMap2 = new HashMap<String, String[]>();
			String ContentType = StringUtils.trim(request.getContentType());
			Map requestMap = NetUtils.urlQueryStrToMap(
					request.getQueryString(), "utf-8");
			bodyMap1 = request.getParameterMap();

			parmMap.putAll(requestMap);
			parmMap.putAll(bodyMap1);

			String protocal = StringUtils.trim(ArrayUtils.getFirst((String[]) parmMap
					.get("Protocol")));
			Protocol requestBodyWithStreamClass = Exchange.geProtocol(protocal);
			if (requestBodyWithStreamClass instanceof ProBodyStream) {
				bodyBytes.setValue(IOUtils.toByteArray(request.getInputStream(), true));

			} else {

				String body = IOUtils.toString(request.getInputStream(),
						"utf-8", true);
				log.debug(ContentType + "+++ request Body:" + body);
				bodyMap2 = NetUtils.urlQueryStrToMap(body, "utf-8");


				
			}
			parmMap.putAll(bodyMap2);

			if (log.isDebugEnabled()) {
				try {
					log.debug("query url-->Map="+ CollectionUtils.toString(requestMap));
					log.debug("ParameterMap=" + CollectionUtils.toString(bodyMap1));
					log.debug("body-->Map=" + CollectionUtils.toString(bodyMap2));
					log.debug("final Map="+CollectionUtils.toString(parmMap));
				} catch (Exception e) {
					log.error("", e);
				}
			}

		} catch (Exception e) {
			log.error("" + CollectionUtils.toString(parmMap), e);
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s = "aa_bb_cc_ _dd";
		System.out.println(ArrayUtils.toJsonString(s.split("_")));

	}

}
